﻿using PetaPoco;
using Qy.Common;
using Qy.Models;
using System.Threading.Tasks;

namespace Qy.Repository
{
    [AppService]
    public class LogLoginRepository : BaseRepository<LogLogin, PetaPocoContext>
    {

        public LogLoginRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }


        public async Task<Page<ViewLogLogin>> GetSqlListAsync(int page, int limit, int uid, string orderBy)
        {
            Sql sql = Sql.Builder;
            sql.Select("A.Id,B.Username,B.Nickname,A.UserId,A.Ip,A.Time,dept.DeptName")
                .From("qy_log_login as A")
                .LeftJoin("qy_admin_user as B").On("A.UserId = B.Id");
            sql.LeftJoin("qy_admin_dept as dept").On("dept.Id=B.DeptId");
            if (uid != 0)
                sql.Where("A.UserId = @0", uid);

            if (!string.IsNullOrEmpty(orderBy))
                sql.OrderBy(orderBy);

            return await CreateDAO().PageAsync<ViewLogLogin>(page, limit, sql);
        }

        public async Task<int> Clear()
        {
            return await CreateDAO().ExecuteAsync("TRUNCATE qy_log_login");
        }
    }
}
